package com.bw.day03.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.net.mvvm.BaseFragment
import com.bw.base.net.param.OrderDetail
import com.bw.base.net.param.ParamPreOrder
import com.bw.day03.R
import com.bw.day03.adapter.CarAdapter
import com.bw.day03.databinding.FragmentCarBinding


class CarFragment : BaseFragment<FragmentCarBinding, CarViewModel>() {
    lateinit var carAdapter: CarAdapter
    var flag=false
    override fun initView() {
        carAdapter= CarAdapter()
        binding.rvCar.adapter=carAdapter
        binding.rvCar.layoutManager= LinearLayoutManager(context)
        viewModel.car("true","10","1")
        carAdapter.addChildClickViewIds(R.id.car_cb,R.id.car_sub,R.id.car_add,R.id.car_delete)
        carAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.car_cb->{
                    carAdapter.data[position].isCheck=!carAdapter.data[position].isCheck
                }
                R.id.car_sub->{
                    carAdapter.data[position].cartNum--
                   ( carAdapter.getViewByPosition(position,R.id.car_num)as TextView).text="${carAdapter.data[position].cartNum}"
                    if(carAdapter.data[position].cartNum==1){
                        (carAdapter.getViewByPosition(position,R.id.car_sub) as TextView).isEnabled=false
                    }
                    viewModel.updateCar(carAdapter.data[position].id,carAdapter.data[position].cartNum)
                }
                R.id.car_add->{
                    carAdapter.data[position].cartNum++
                    ( carAdapter.getViewByPosition(position,R.id.car_num)as TextView).text="${carAdapter.data[position].cartNum}"

                        (carAdapter.getViewByPosition(position,R.id.car_sub) as TextView).isEnabled=true
                    viewModel.updateCar(carAdapter.data[position].id,carAdapter.data[position].cartNum)
                }

                R.id.car_delete->{
                    viewModel.deleteCar(arrayOf(carAdapter.data[position].id))
                    carAdapter.data.removeAt(position)
                    carAdapter.notifyItemRemoved(position)
                }
            }
            checkBottom()

        }
        binding.cbAll.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.cbAll.setOnClickListener {
                carAdapter.data.forEach {
                    it.isCheck=isChecked
                }
                carAdapter.notifyDataSetChanged()
                checkBottom()
            }
        }
        binding.tvManage.setOnClickListener {
            if (binding.tvManage.text.toString().equals("管理")){
                binding.tvManage.text="取消"
                binding.llCancel.isVisible=true
                binding.llMang.isVisible=false
            }else{
                binding.tvManage.text="管理"
                binding.llCancel.isVisible=false
                binding.llMang.isVisible=true
            }
        }
        binding.tvDelete.setOnClickListener {
            val list=mutableListOf<Boolean>()
            carAdapter.data.forEach {
                list.add(it.isCheck)
            }
            if(true in list){
                flag=true
                var ids=arrayOf<Int>()
                carAdapter.data.forEach {
                    if (it.isCheck==true){
                        ids+=it.id
                    }
                }
                viewModel.deleteCar(ids)

            }else{
                ToastUtils.showLong("请选择要删除的购物车商品")
            }
        }


    }

    private fun checkBottom() {
        var amount=0f
        var total=0
        carAdapter.data.forEach {
            if (it.isCheck){
                total++
                amount+=it.cartNum*it.price.toFloat()
            }
        }
        binding.amountAll.text="¥$amount"
        binding.cbAll.text="全选($total)"
        binding.tvCount.text="购物数量${carAdapter.data.size}"
        val list=mutableListOf<Boolean>()
        carAdapter.data.forEach {
            list.add(it.isCheck)
        }
        if(false in list){
            binding.cbAll.isChecked=false
        }else{
            binding.cbAll.isChecked=true
        }
        binding.orderNow.setOnClickListener {
            val list=mutableListOf<Boolean>()
            carAdapter.data.forEach {
                list.add(it.isCheck)
            }
            if (true in list){
                var paramPreOrder= ParamPreOrder()
                paramPreOrder.preOrderType="shoppingCart"
                val orderList=mutableListOf<OrderDetail>()
                carAdapter.data.forEach {
                    if (it.isCheck){
                        val orderDetail= OrderDetail()
                        orderDetail.shoppingCartId=it.id
                        orderList.add(orderDetail)
                    }
                }
                paramPreOrder.orderDetails=orderList
                viewModel.preOrder(paramPreOrder)
            }else{
                ToastUtils.showLong("请选择要结算的购物车商品")
            }

        }
    }

    override fun initData() {
        viewModel.carSuccess.observe(this){
            if (it.code==200){
                carAdapter.data.clear()
                carAdapter.data.addAll(it.data.list)
                carAdapter.notifyDataSetChanged()
                binding.tvCount.text="购物数量${it.data.list.size}"
            }
        }
        viewModel.updateSuccess.observe(this){
            ToastUtils.showLong(it.message)

        }
        viewModel.deleteSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if (it.code==200){
                if (flag){
                    flag=false
                    viewModel.car("true","10","1")
                    binding.cbAll.text="全选(0)"
                    binding.tvCount.text="购物数量0"
                    checkBottom()
                }

            }
        }
        viewModel.preSuccess.observe(this){
            if (it.code==200){
                ARouter.getInstance().build("/app/CreateOrderActivity").withString("preOrderNo",it.data.preOrderNo).navigation()
            }
        }

    }

    override fun getViewModel(): Class<CarViewModel> = CarViewModel::class.java

    override fun getLayoutId(): Int = R.layout.fragment_car


}